<template>
  <div class="mainbox">
    <el-button type="info" icon="el-icon-plus" @click="handleAdd('1')"
      >新增菜品分类</el-button
    >
    <el-button type="primary" icon="el-icon-plus" @click="handleAdd('2')"
      >新增套餐分类</el-button
    >
    <div class="table">
      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column prop="name" label="分类名称"> </el-table-column>
        <el-table-column prop="type" label="分类类型">
          <template slot-scope="scope">
            {{ scope.row.type === 1 ? "菜品分类" : "套餐分类" }}
          </template>
        </el-table-column>
        <el-table-column prop="updateTime" label="操作时间"> </el-table-column>
        <el-table-column prop="sort" label="排序"> </el-table-column>
        <el-table-column prop="address" label="操作">
          <template slot-scope="scope">
            <el-link type="primary" @click="handleChange(scope.row)"
              >修改</el-link
            >
            <el-divider direction="vertical"></el-divider>
            <el-link type="danger" @click="open">删除</el-link>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <Pagination  
    @pagination="init"
    :total="total"
    :page.sync="pageInfo.page"
    :limit.sync="pageInfo.pageSize"
    />
    
    <el-dialog
      :title="`${isEdit ? '编辑' : '新增'}${
        form.type === 1 ? '菜品' : '套餐'
      }分类`"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="分类名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="排序">
          <el-input v-model="form.sort"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSave">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getClassify, addClassify} from "@/api/classify";
import Pagination from "@/components/Pagination"
export default {
  data() {
    return {
      tableData: [],
      total: 0,
      pageInfo: {
        page: 1,
        pageSize: 10,
      },
      isEdit: false,
      dialogVisible: false,
      form: {
        name: "",
        sort: "",
        type: "1",
      },
    };
  },
  methods: {
    init(){
      getClassify(this.pageInfo).then((res)=>{
        this.tableData = res.data.records;
        this.total = res.data.total;
      })
    },
    handleAdd(type) {
      this.form.type = type;
      this.dialogVisible = true;
    },
    handleChange(row) {
      this.dialogVisible = true;
      this.isEdit = true;
      this.form = row;
    },
    open() {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleSave() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.isEdit) {
          } else {
            addClassify(this.form).then((res) => {
              this.$message.success("新增成功");
              this.dialogVisible = false;
              this.form = {};
            });
          }
        } else {
          return false;
        }
      });
    },
  },
  created() {
    this.init();
  },
  components:{Pagination},
};
</script>

<style>
.table {
  margin-top: 10px;
}


</style>
